//
//  NSDate+WJNSDateExt.m
//  weibo
//
//  Created by 王俊 on 16/1/10.
//  Copyright © 2016年 王俊. All rights reserved.
//

#import "NSDate+WJNSDateExt.h"

@implementation NSDate (WJNSDateExt)

- (BOOL)isToday
{
    NSCalendar *canlendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit =  NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *currentComp =  [canlendar components:unit fromDate:[NSDate date]];
    NSDateComponents *selfComp = [canlendar components:unit fromDate:self];
    return (currentComp.year == selfComp.year)&&(currentComp.month == selfComp.month)&&(currentComp.day == selfComp.day);
    
}
- (BOOL)isThisYear
{
    NSCalendar *canlendar = [NSCalendar currentCalendar];
    NSCalendarUnit unit =  NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
    NSDateComponents *currentComp =  [canlendar components:unit fromDate:[NSDate date]];
    NSDateComponents *selfComp = [canlendar components:unit fromDate:self];
    return (currentComp.year == selfComp.year);
    
}

- (BOOL)isYesterday
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *nowDate = [[NSDate date] dateWithYMD];
    NSDate *selfDate = [self dateWithYMD];
    NSDateComponents *comps = [calendar components:NSCalendarUnitDay fromDate:selfDate toDate:nowDate options:0];
    return (comps.day == 1);
}

- (NSDate *)dateWithYMD
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *selfString = [formatter stringFromDate:self];
    return [formatter dateFromString:selfString];
}

- (NSDateComponents *)deltaWithNow
{
    NSCalendar   *calendar = [NSCalendar currentCalendar];
    NSDate *nowDate = [NSDate date];
    NSCalendarUnit unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    return [calendar components:unit fromDate:self toDate:nowDate options:0];
}

@end
